package com.codingvip.sort;

/**
 * @Author Tony.Han
 * @Created at 2020/4/4 8:43 上午
 * Desc:
 */
public class BucketSort {

    public static  int[] bucketSort(int a[]){
        int[] splitBucket = new int[100];
        for (int i=0;i<a.length;i++) {
            int originValue = a[i];
            splitBucket[originValue]=splitBucket[originValue]+1;
        }
        int k=0;
        int nums = 0;
        for (int j = 0; j < splitBucket.length; j++) {
            nums=splitBucket[j];
            if (nums > 0) {
                for (int i = 0; i < nums; i++) {
                    a[k] = j;
                    k++;
                }
            }
        }
        return a;
    }

    public static void main(String[] args) {
        int []the_array = {10,1,18,30,23,12,7,5,18,17};
        System.out.print("之前的排序：");
        for(int i = 0; i < the_array.length; i++) {
            System.out.print(the_array[i] + " ");
        }
        System.out.println();
        int []result_array = bucketSort(the_array);

        System.out.print("桶排序：");
        for(int i = 0; i < result_array.length; i++) {
            System.out.print(result_array[i] + " ");
        }
    }
}
